package com.wwj.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

/**
 * Created by xya on 16-6-13.
 */
public class IDGenerator {
    private static long num = 0;

    /**
     * 随机生成UUID
     *
     * @return
     */
    public static synchronized String getUUID() {
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString().toUpperCase();
        String uuidStr = str.replace("-", "");
        return uuidStr;
    }

    /**
     * 根据字符串生成固定UUID
     *
     * @param key
     * @return
     */
    public static synchronized String getUUID(String key) {
        UUID uuid = UUID.nameUUIDFromBytes(key.getBytes());
        String str = uuid.toString().toUpperCase();
        String uuidStr = str.replace("-", "");
        return uuidStr;
    }

    /**
     * 根据日期生成长整型id
     *
     * @param
     */
    public static synchronized long getLongId() {
        String date = new SimpleDateFormat("yyyyMMddHHmmssS").format(Calendar.getInstance().getTime());
        System.out.println("原始id=" + date);
        if (num >= 99) num = 0l;
        ++num;
        if (num < 10) {
            date = date + 00 + num;
        } else {
            date += num;
        }
        return Long.valueOf(date);
    }

}
